package cn.caplike.demo.consistence.threadpool.and.blockingqueue.core.configuration;

import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * 主要是用来创建RedisTemplate的实例对象
 */
@Configuration
public class RedisConfig {

    /**
     * 系统初始化一个String,Object的redisTemplate对象，便于操作数据
     * 使用fastjson来序列化
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        /*
         * 使用fastJson 来序列化
         */
        FastJsonRedisSerializer<?> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
        // value值使用fastjson序列化
        redisTemplate.setValueSerializer(fastJsonRedisSerializer);
        // hash值使用fastjson序列化
        redisTemplate.setHashValueSerializer(fastJsonRedisSerializer);
        // key的值使用StringRedisSerializer来序列化
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        return redisTemplate;

    }
}
